package com.persondemo.videoappliction.ui.search;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.persondemo.videoappliction.R;
import com.persondemo.videoappliction.bean.VipSeachBean;
import com.persondemo.videoappliction.component.ApplicationComponent;
import com.persondemo.videoappliction.component.DaggerHttpComponent;
import com.persondemo.videoappliction.ui.adapter.SeachVipAdapter;
import com.persondemo.videoappliction.ui.base.BaseActivity;
import com.persondemo.videoappliction.ui.search.contract.SeachVipContract;
import com.persondemo.videoappliction.ui.search.presenter.SeachVipPresenter;
import com.persondemo.videoappliction.utils.T;
import com.persondemo.videoappliction.widget.CustomLoadMoreView;
import com.persondemo.videoappliction.widget.SimpleMultiStateView;
import com.persondemo.videoappliction.widget.SpaceItemDecoration;
import com.persondemo.videoappliction.widget.searchview.ICallBack;
import com.persondemo.videoappliction.widget.searchview.SearchView;
import com.persondemo.videoappliction.widget.searchview.bCallBack;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

/**
 * Created by Administrator on 2018/4/16.
 */
public class SeachVipActivity extends BaseActivity<SeachVipPresenter> implements SeachVipContract.View {
    @BindView(R.id.searchvip_view)
    SearchView searchvipView;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.SimpleMultiStateView)
    com.persondemo.videoappliction.widget.SimpleMultiStateView SimpleMultiStateView;
    @BindView(R.id.mPtrFrameLayout)
    PtrClassicFrameLayout mPtrFrameLayout;
    private int page = 1;
    private List<VipSeachBean.DataBean> list;
    private SeachVipAdapter adapter;
    private String search = "";

    @Override
    public int getContentLayout() {
        return R.layout.module_activity_vipseach;
    }

    @Override
    public void initInjector(ApplicationComponent appComponent) {
        DaggerHttpComponent.builder()
                .applicationComponent(appComponent)
                .build()
                .inject(this);
    }

    @Override
    public void bindView(View view, Bundle savedInstanceState) {
        mPtrFrameLayout.disableWhenHorizontalMove(true);
        mPtrFrameLayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, mRecyclerView, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                page = 1;
                if (mPresenter != null) {
                    mPresenter.getseachdata(search, page);
                }
            }
        });

    }

    @Override
    public void initData() {
        list = new ArrayList<>();
        Intent intent = getIntent();
        if (intent != null) {
            search = intent.getStringExtra("title");
            page = 1;
            if (mPresenter != null) {
                showLoadingDialog();
                mPresenter.getseachdata(search, page);
            }
        }
        // 4. 设置点击搜索按键后的操作（通过回调接口）
        // 参数 = 搜索框输入的内容
//        searchvipView.
        searchvipView.setOnClickSearch(new ICallBack() {
            @Override
            public void SearchAciton(String string) {
                showLoadingDialog();
                page = 1;
                mPresenter.getseachdata(string, page);
                search = string;
            }
        });
        // 5. 设置点击返回按键后的操作（通过回调接口）
        searchvipView.setOnClickBack(new bCallBack() {
            @Override
            public void BackAciton() {
                finish();
            }
        });
        searchvipView.setOnClickListener(new Oncliseter() {
            @Override
            public void onItemClickListener(View v, String title) {
                showLoadingDialog();
                page = 1;
                mPresenter.getseachdata(title, page);
                search = title;
            }
        });
        mRecyclerView.addItemDecoration(new SpaceItemDecoration(10));
    }

    @Override
    public void loaddata(VipSeachBean vipSeachBean) {
        hideLoadingDialog();
        if (vipSeachBean == null || vipSeachBean.getStatus() != 1) {
            showFaild();
            return;
        }
        if (vipSeachBean.getData() == null || vipSeachBean.getData().size() == 0) {
            T.showShort(SeachVipActivity.this, "没有更多数据了！");
            return;
        }
        if (vipSeachBean.getData().size() > 0 && vipSeachBean.getStatus() == 1) {
            list.clear();
            list = vipSeachBean.getData();
            mRecyclerView.setLayoutManager(new GridLayoutManager(SeachVipActivity.this, 1));
            adapter = new SeachVipAdapter(list, SeachVipActivity.this);
            adapter.setNewData(vipSeachBean.getData());
            mRecyclerView.setAdapter(adapter);
            mRecyclerView.addItemDecoration(new SpaceItemDecoration(10));
            adapter.setEnableLoadMore(true);
            adapter.setLoadMoreView(new CustomLoadMoreView());
            adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
            adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                    if (position > 3) {
//                        return;
//                    }
                    if (TextUtils.isEmpty(list.get(position).getHref())) {
                        T.showShort(SeachVipActivity.this, "资源为空，请选择其他资源！");
                        return;
                    }
                    MobclickAgent.onEvent(SeachVipActivity.this, "search");
                    Intent intent = new Intent(SeachVipActivity.this, SeachWebActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("title", list.get(position).getHref());
                    intent.putExtras(bundle);
                    startActivity(intent);
                    overridePendingTransition(R.anim.slide_in_left, R.anim.slide_in_right);
                }
            });
//            adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
//                @Override
//                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
//                    if (TextUtils.isEmpty(list.get(position).getHref())) {
//                        T.showShort(SeachVipActivity.this, "资源为空，请选择其他资源！");
//                        return;
//                    }
//                    Intent intent = new Intent(SeachVipActivity.this, SeachWebActivity.class);
//                    Bundle bundle = new Bundle();
//                    bundle.putString("title", list.get(position).getHref());
//                    intent.putExtras(bundle);
//                    startActivity(intent);
//                    overridePendingTransition(R.anim.slide_in_left, R.anim.slide_in_right);
//                }
//            });
            adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                @Override
                public void onLoadMoreRequested() {
                    mPresenter.getseachdata(search, page);
                }
            }, mRecyclerView);
            adapter.disableLoadMoreIfNotFullPage(mRecyclerView);
            mPtrFrameLayout.refreshComplete();
            showSuccess();
            page++;
            if (list.size() < 3) {
                adapter.loadMoreEnd();
            }

        } else {
            T.showShort(SeachVipActivity.this, vipSeachBean.getMsg());
        }
    }

    @Override
    public void getmoredata(VipSeachBean vipSeachBean) {
        if (vipSeachBean.getData() == null || vipSeachBean.getData().size() == 0) {
            adapter.loadMoreEnd();
            T.showShort(SeachVipActivity.this, "没有更多数据了！");
            return;
        } else {
            page++;
            adapter.addData(vipSeachBean.getData());
            adapter.loadMoreComplete();
        }
    }

    @Override
    public void onRetry() {
    }

    @Override
    public boolean isSupportSwipeBack() {
        return false;
    }
}
